/**
 * Created by Ljh on 2016/12/7.
 */
//豆瓣跨域的方法
angular.module('crossModule.service', [])
	.service('crossService',['$window',function ($window) {
		this.jsonp = function (url,params,fn) {
			var callback = "jsonp_" + (Math.random()*Math.random()).toString().substr(2);
			console.log(callback);
			$window[callback] = function (data) {
				fn(data);
				$window.document.body.removeChild(scriptElement);
			}
			var queryStr = '';
			//拼接请求参数
			for( var key in params ){
				queryStr += key + '=' + params[key] + '&';
			}
			//拼接回调函数
			queryStr += 'callback' + '=' + callback;
			url += '?' + queryStr;
			console.log(url);
			var scriptElement = $window.document.createElement('script');
			scriptElement.src = url;
			$window.document.body.appendChild(scriptElement);
		}
	}])
